######################
# Appendix: GERMANY  #    
# Figure 6 & 10      #
######################

###load library
library(openxlsx)
library(psych)
library(ggplot2)
library(gridExtra)
library(dtw)
library(tseries)
library(TSclust)
library(tidyverse)
library(zoo)
library(xtable)
source("extract.R")


#######----------Figure 6: All patterns of indices (Germany)--------##########


#######################Laakso Taagepera ENPP###########################

df_meangr<-read.csv("GERMAN_FINAL.csv")
attach(df_meangr)

#calculating the sum of percentage and the percentage of the independent
allp_gr=.n+ADM_ALL+AFD_AL+ALFA+andere.P+BP.Bay+BundFre+Bundnis+Burgerpa+BuSo.Bu+
  CDU+CM_Chri+DA+DasNeue+DBD+DieViol+DKP+DSU+DVU+Familie+Feminist+FWD_Fr+
  GAZ+GLU+Graue.Pa+Grune+MLPD_M+NPD+ODP+ODPD+PBC_Pa+PDS.die+Piratenp+PSG_Pa+
  Rentnerp+Republik+RRP...Re+Schill.P+SPD+Statt.Pa+Tierschu+WASG_w+Zentrum
indep_gr=1-allp_gr

#calculating macropartisanship 
df_meangr$macropartisanship1<-(CDU/allp_gr)*(effpar_ele/2)*((allp_gr/indep_gr)*(1/effpar_ele^2))
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#weighting process
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

df_meangr$macropartisanship_GR_LT<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)


###-------without nested s2

#calculating macropartisanship 
df_meangr$macropartisanship1<-(CDU/allp_gr)*(effpar_ele/2)*(allp_gr/indep_gr)
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#weighting process
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

df_meangr$macropartisanship_GR_LT_withoutnested<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)

###-----simply product the proportion of independent 


#calculating macropartisanship 
df_meangr$macropartisanship1<-(CDU/allp_gr)*(effpar_ele/2)*indep_gr
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#weighting process
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

df_meangr$macropartisanship_GR_LT_prindependent<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)





###########################Molinar############################

###-----The simple Molinar ENPP

#calculating macropartisanship 
df_meangr$macropartisanship1<-(CDU/allp_gr)*(enpp_np/2)*((allp_gr/indep_gr)*(1/enpp_np^2))
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#weighting process
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

df_meangr$macropartisanship_GR_NP<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)

###-------without nested s2

#calculating macropartisanship 
df_meangr$macropartisanship1<-(CDU/allp_gr)*(enpp_np/2)*(allp_gr/indep_gr)
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#weighting process
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

df_meangr$macropartisanship_GR_NP_withoutnested<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)


###-----simply product the proportion of independent 


#calculating macropartisanship 
df_meangr$macropartisanship1<-(CDU/allp_gr)*(enpp_np/2)*indep_gr
describe(df_meangr$macropartisanship1)
df_meangr$perCDU<-CDU/allp_gr

#weighting process
df_meangr$macrop_var<-var(df_meangr$macropartisanship1)
df_meangr$macroper_var<-var(df_meangr$perCDU)

df_meangr$macropartisanship_GR_NP_prindependent<-(df_meangr$macropartisanship1/df_meangr$macrop_var+df_meangr$perCDU/df_meangr$macroper_var)/
  (1/df_meangr$macrop_var+1/df_meangr$macroper_var)



###constituting data frame for comparison

df_mp_enpp<-data.frame(CDU,df_meangr$yearMon,
                       df_meangr$macropartisanship_GR_LT,
                       df_meangr$macropartisanship_GR_LT_withoutnested,
                       df_meangr$macropartisanship_GR_LT_prindependent,
                       df_meangr$macropartisanship_GR_NP,
                       df_meangr$macropartisanship_GR_NP_withoutnested,
                       df_meangr$macropartisanship_GR_NP_prindependent)

##################mp with DRA##############################


#load data
ge<-read.csv("german_extract.csv")
attach(ge)
describe(ge)
ge<-na.omit(ge)

#make tidy data by tidyverse
df_german_tidy <- ge %>%
  gather(key = VARNAME, value = value, -yearMon)

#setting time-series information
varname<-df_german_tidy$VARNAME
date<-as.Date(df_german_tidy$yearMon)
index<-df_german_tidy$value
ncases<-NULL

#using "extract" to compute macropartisanship
output_german<-extract(varname,date=date,index,ncases,begindt=ISOdate(1977,3,1),
                       enddt = ISOdate(2016,12,1), unit="M",npass=2)
display(output_german)
summary(output_german)

df_ld_german<-data.frame(output_german$varname, output_german$loadings1,output_german$loadings2)
colnames(df_ld_german)<-c("Party names","Loadings1","Loadings2")
print(xtable(df_ld_german), include.rownames=FALSE)

#factor scores of 2 dimensions
mpartisan1_german_DRA<-output_german$latent1
mpartisan2_german<-output_german$latent2

yearMon<-as.Date(sprintf("%.2f.01", output_german$period), format = "%Y.%m.%d")

###constituting data frame for comparison
df_dra_german<-data.frame(as.factor(yearMon),mpartisan1_german_DRA)
colnames(df_dra_german)<-c("df_meangr.yearMon","mpartisan1_german_DRA")

#join two data.frame
tb_german_mpcomparison<-inner_join(df_dra_german,df_mp_enpp,by=c("df_meangr.yearMon"))
write.csv(tb_german_mpcomparison, "tb_german_mpcomparison.csv")


##################plot all indices#####################
german_fig2<-tb_german_mpcomparison 
colnames(german_fig2)<-c( "df_meangr.yearMon","DRA","CDU","LT-nested","LT-simple","LT-indep","NP-nested","NT-simple","NP-independent")


#plot the simple macropartisanship and the modified
tidy_all_indices<-german_fig2 %>% 
  gather(key="variable", value="value", -df_meangr.yearMon)

german_compare<-ggplot(tidy_all_indices, aes(as.yearmon(x = df_meangr.yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  theme_minimal()
ggsave(plot=german_compare,filename="german_compare.pdf",width=12,height=8)



#######---------Figure 9: Dynamic time warping distance for the German case----------########

##################DTW############################

windows(30,25)
pdf("fig_dtw_german.pdf")
split.screen(c(3,3))

###----displaying dynamic time warping:

###LT indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_german_mpcomparison$CDU ,tb_german_mpcomparison$df_meangr.macropartisanship_GR_LT_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_german_mpcomparison.CDU
ts_b<-df_dtw$tb_german_mpcomparison.df_meangr.macropartisanship_GR_LT_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(1)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-indep", cex.main = 0.6)


###NP indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_german_mpcomparison$CDU ,tb_german_mpcomparison$df_meangr.macropartisanship_GR_NP_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_german_mpcomparison.CDU
ts_b<-df_dtw$tb_german_mpcomparison.df_meangr.macropartisanship_GR_NP_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(2)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-indep", cex.main = 0.6)


###LT simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_german_mpcomparison$CDU ,tb_german_mpcomparison$df_meangr.macropartisanship_GR_LT_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_german_mpcomparison.CDU
ts_b<-df_dtw$tb_german_mpcomparison.df_meangr.macropartisanship_GR_LT_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance



screen(3)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-simple", cex.main = 0.6)


###NP simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_german_mpcomparison$CDU ,tb_german_mpcomparison$df_meangr.macropartisanship_GR_NP_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_german_mpcomparison.CDU
ts_b<-df_dtw$tb_german_mpcomparison.df_meangr.macropartisanship_GR_NP_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(4)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-simple", cex.main = 0.6)


###LT nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_german_mpcomparison$CDU ,tb_german_mpcomparison$df_meangr.macropartisanship_GR_LT)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_german_mpcomparison.CDU
ts_b<-df_dtw$tb_german_mpcomparison.df_meangr.macropartisanship_GR_LT

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(5)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-nested", cex.main = 0.6)


###NP nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_german_mpcomparison$CDU ,tb_german_mpcomparison$df_meangr.macropartisanship_GR_NP)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_german_mpcomparison.CDU
ts_b<-df_dtw$tb_german_mpcomparison.df_meangr.macropartisanship_GR_NP

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(6)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-nested", cex.main = 0.6)




####DRA
#simply calulating dtw distance
df_dtw<-data.frame(tb_german_mpcomparison$CDU ,tb_german_mpcomparison$mpartisan1_german_DRA)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_german_mpcomparison.CDU
ts_b<-df_dtw$tb_german_mpcomparison.mpartisan1_german_DRA

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(7)


dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs DRA", cex.main = 0.6)


dev.off()




